﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using vHCS.HR.Entities;

namespace vHCS.Web.MdlHR.Controls
{
    /// <summary>
    /// HungDV
    /// </summary>
    public partial class HR_ctrAnswers : System.Web.UI.UserControl
    {
        protected override void OnPreRender(EventArgs e)
        {
            //Nếu bắt buộc nhập
            if (this.QuestionSelectType == QuestionsSelectType.None)
            {
            }
            if (this.IsRequire)
            {
                this.valRequireCheckBox.ValidationGroup = this.ValidationGroup;
                this.valRequireComboAnswer.ValidationGroup = this.ValidationGroup;               
                this.valRequireRadio.ValidationGroup = this.ValidationGroup;
                this.valRequieComment.Visible = false;
                this.valRequieMultiComment.Visible = false;

                //Câu hỏi bắt buộc nhưng không hiển thị dạng chọn
                //Bắt buộc nhập text
                if (this.QuestionSelectType == QuestionsSelectType.None) {
                    if (this.QuestionInputType == QuestionsInputType.TextBox)
                    {
                        this.valRequieComment.ValidationGroup = this.ValidationGroup;
                        this.valRequieComment.Visible = true;
                    }
                    if (this.QuestionInputType == QuestionsInputType.EditorTextBox)
                    {
                        this.valRequieMultiComment.ValidationGroup = this.ValidationGroup;
                        this.valRequieMultiComment.Visible = true;
                    }
                }
            }
            else {
                string sGroup = DateTime.Now.ToString();
                this.valRequireCheckBox.ValidationGroup = sGroup;
                this.valRequireComboAnswer.ValidationGroup = sGroup;
                this.valRequireRadio.ValidationGroup = sGroup;
                this.valRequieComment.ValidationGroup = sGroup;
                this.valRequieMultiComment.ValidationGroup = sGroup;

                this.valRequieComment.Visible = false;
                this.valRequieMultiComment.Visible = false;
                this.valRequireCheckBox.Visible = false;
                this.valRequireComboAnswer.Visible = false;
                this.valRequireRadio.Visible = false;
            }
            base.OnPreRender(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
               
            }
        }

        /// <summary>
        /// Tên field chứa câu trả lời
        /// </summary>
        public string DataTextField
        {
            get
            {
                return this.hdfDataTextField.Value;
            }
            set
            {
                this.hdfDataTextField.Value = value;
            }
        }

        /// <summary>
        /// Tên Field chứa mã câu trả lời
        /// </summary>
        public string DataValueField
        {
            get
            {
                return this.hdfDataValueField.Value;
            }
            set
            {
                this.hdfDataValueField.Value = value;
            }
        }

        /// <summary>
        /// Nhóm validate
        /// </summary>
        public string ValidationGroup
        {
            get
            {
                return this.hdfValidationGroup.Value;
            }
            set
            {
                this.hdfValidationGroup.Value = value;
                this.valRequireCheckBox.ValidationGroup = this.ValidationGroup;
                this.valRequireComboAnswer.ValidationGroup = this.ValidationGroup;
                this.valRequireRadio.ValidationGroup = this.ValidationGroup;
            }
        }
               
        /// <summary>
        ///Mã câu hỏi
        /// </summary>
        private long QuestionId
        {
            get {
                if (string.IsNullOrEmpty(this.hdfQuestionId.Value))
                    return 0;
                else
                    return long.Parse(this.hdfQuestionId.Value.Trim());
            }
            set { this.hdfQuestionId.Value = value.ToString(); }
        }

        /// <summary>
        /// Số thứ tự câu hỏi
        /// Bắt đầu bằng 1
        /// </summary>
        public int Index {
            set {
                this.hdfIndex.Value = value.ToString();
                this.lblQuesionIndex.Text = value.ToString();
            }
            get {
                int iIndex = 0;
                int.TryParse(this.hdfIndex.Value, out iIndex);
                return iIndex;
            }
        }

        /// <summary>
        /// Câu hỏi bắt buộc trả lời
        /// </summary>
        private bool IsRequire
        {
            set { 
                this.hdfRequire.Value = value.ToString();
                if (value)
                {
                    this.lblRequire.Visible = true;
                    
                }
                else { 
                    this.lblRequire.Visible = false;                                  
                }
            }
            get
            {
                bool bRequire = false;
                bool.TryParse(this.hdfRequire.Value, out bRequire);
                return bRequire;
            }
        }

        /// <summary>
        /// Loại câu hỏi dạng chọn
        /// </summary>
        private QuestionsSelectType QuestionSelectType
        {
            get
            {
                if (string.IsNullOrEmpty(this.hdfQuestionType.Value))
                    return QuestionsSelectType.None;
                else {
                    QuestionsSelectType type = QuestionsSelectType.None;
                    Enum.TryParse<QuestionsSelectType>(this.hdfQuestionType.Value, out type);
                    return type;
                }
            }
            set
            {
                this.hdfQuestionType.Value = value.ToString();

                this.dtlRadioAnswerList.Visible = false;               
                this.pnlComboAnswer.Visible = false;
                this.dtlCheckBoxAnswerList.Visible = false;
                
                if (value == QuestionsSelectType.Radio)
                {
                    this.dtlRadioAnswerList.Visible = true;
                }
                else if (value == QuestionsSelectType.ComboBox)
                {
                    this.pnlComboAnswer.Visible = true;
                }
                else if (value == QuestionsSelectType.CheckBox)
                {
                    this.dtlCheckBoxAnswerList.Visible = true;
                }
               
            }
        }

        /// <summary>
        /// Loại câu hỏi dạng input
        /// </summary>
        private QuestionsInputType QuestionInputType
        {
            get
            {
                if (string.IsNullOrEmpty(this.hdfQuestionInputType.Value))
                    return QuestionsInputType.None;
                else
                {
                    QuestionsInputType type = QuestionsInputType.None;
                    Enum.TryParse<QuestionsInputType>(this.hdfQuestionInputType.Value, out type);
                    return type;
                }
            }
            set { 
                this.hdfQuestionInputType.Value=value.ToString();
                this.pnlEditor.Visible=false;
                this.pnlTextBox.Visible=false;

                if(value==QuestionsInputType.TextBox)
                    this.pnlTextBox.Visible = true;
                if (value == QuestionsInputType.EditorTextBox)
                    this.pnlEditor.Visible = true;
            }
        }

        /// <summary>
        /// Danh sách câu trả lời
        /// Trả về danh sách câu trả lời bao gồm kết quả trả lời của enduser
        /// </summary>
        public List<HR_AnswerDTO> DataSource {
            set {
                ListItem[] itemCollection =null;
                if (value != null) {
                    int  index=0;
                    //Nếu không bắt buộc nhập và không phải là checkbox list thì thêm phần tử rỗng
                    if (!this.IsRequire && this.QuestionSelectType != QuestionsSelectType.CheckBox )
                    {
                        itemCollection = new ListItem[value.Count + 1];
                        itemCollection[0] = new ListItem(this.lblNALabel.Text,"0");
                        index++;
                    }
                    else 
                        itemCollection = new ListItem[value.Count];
                    foreach (HR_AnswerDTO dto in value) {
                        ListItem item = new ListItem(dto.AnswerName,dto.AnswerID.ToString());
                        item.Selected = dto.Selected;
                        itemCollection[index] = item;
                        index++;
                    }

                }
                //Gán source theo các loại câu hỏi tương ứng
                if (this.QuestionSelectType == QuestionsSelectType.Radio)
                    this.dtlRadioAnswerList.Items.AddRange(itemCollection);
                else if (this.QuestionSelectType == QuestionsSelectType.ComboBox)
                    this.cboComboAnswer.Items.AddRange(itemCollection);
                else if (this.QuestionSelectType == QuestionsSelectType.CheckBox)
                    this.dtlCheckBoxAnswerList.Items.AddRange(itemCollection);
                else if (this.QuestionInputType == QuestionsInputType.TextBox & itemCollection.Length > 1)
                {
                    this.dtlTextboxList.DataSource = itemCollection;

                }
            }
            
        }

        public bool Contains(HR_AnswerDTO dto, List<HR_AnswerDTO> lstDTO)
        {
            foreach (HR_AnswerDTO item in lstDTO)
                if (item.QuestionId == dto.QuestionId) return true;
            return false;
        }
        /// <summary>
        /// Thông tin câu trả lời của enduser
        /// </summary>
        public HR_QuestionDTO Question {
            get
            {
                if (this.QuestionId <= 0) return null;
                ListItemCollection itemCollection = new ListItemCollection();
                string[] strAnswers=new string[10] ;
                string[] strAnswersID = new string[10];
                QuestionsSelectType selectType = QuestionsSelectType.None;

                if (this.QuestionSelectType == QuestionsSelectType.Radio)
                {
                    itemCollection = this.dtlRadioAnswerList.Items;
                    selectType = QuestionsSelectType.Radio;
                }
                else if (this.QuestionSelectType == QuestionsSelectType.ComboBox)
                {
                    itemCollection = this.cboComboAnswer.Items;
                    selectType = QuestionsSelectType.ComboBox;
                }
                else if (this.QuestionSelectType == QuestionsSelectType.CheckBox)
                {
                    itemCollection = this.dtlCheckBoxAnswerList.Items;
                    selectType = QuestionsSelectType.CheckBox;
                }
                else if (this.QuestionInputType == QuestionsInputType.TextBox)
                {
                    int i = 0;
                    foreach (DataListItem item in this.dtlTextboxList.Items)
                    {
                        TextBox txtAns = (TextBox)item.FindControl("txtAnsName");
                        Label lblAnsID = (Label)item.FindControl("lblAnswerID");
                        if (txtAns.Text != "")
                        {
                            strAnswers[i] = txtAns.Text;
                            strAnswersID[i] = lblAnsID.Text;
                        }
                        i++;
                       
                    }

                }


                return this.GetOutputQuestion(itemCollection, selectType,strAnswersID,strAnswers);               
            }

            set {
                if (value != null)
                {
                    this.lblQuestionName.Text = value.QuestionName;
                    this.QuestionId = value.QuestionID;
                    this.IsRequire = value.IsRequire;
                    this.QuestionSelectType = value.SelectType;
                    this.QuestionInputType = value.InputType;
                    this.txtComment.Text = value.Comment;
                    this.txtOtherComment.Text = value.Comment;
                    this.DataSource = value.Answers;

                }
            }
        }
        /// <summary>
        /// thông tin câu trả lời dạng nhập của enduser
        /// </summary>
        public HR_QuestionDTO InputQuestion
        {
            get
            {
                if (this.QuestionId <= 0) return null;
                QuestionsSelectType selectType = QuestionsSelectType.None;
                string[] strAnswers = new string[10];
                string[] strAnswersID = new string[10];
  
                 if (this.QuestionInputType == QuestionsInputType.TextBox)
                {
                    int i = 0;
                    foreach (DataListItem item in this.dtlTextboxList.Items)
                    {
                        TextBox txtAns = (TextBox)item.FindControl("txtAnsName");
                        Label lblAnsID = (Label)item.FindControl("lblAnswerID");
                        if (txtAns.Text != "")
                        {
                            strAnswers[i] = txtAns.Text;
                            strAnswersID[i] = lblAnsID.Text;
                        }
                        i++;

                    }

                }


                 return this.GetOutputQuestion(selectType, strAnswersID, strAnswers);
            }

            set
            {
                if (value != null)
                {
                    this.lblQuestionName.Text = value.QuestionName;
                    this.QuestionId = value.QuestionID;
                    this.IsRequire = value.IsRequire;
                    this.QuestionSelectType = value.SelectType;
                    this.QuestionInputType = value.InputType;
                    this.txtComment.Text = value.Comment;
                    this.txtOtherComment.Text = value.Comment;
                    this.DataSource = value.Answers;

                }
            }
        }
        /// <summary>
        /// Lấy thông tin câu trả lời của enduser
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        private HR_QuestionDTO GetOutputQuestion(ListItemCollection items,QuestionsSelectType selectType,string[] strAnsID,string[] strAns)
        {
            HR_QuestionDTO question = new HR_QuestionDTO();
            question.QuestionID = this.QuestionId;
            question.SelectType = selectType;
            question.InputType = QuestionsInputType.None;
            ///Lấy kết quả dạng chọn
            List<HR_AnswerDTO> lstAnswer = new List<HR_AnswerDTO>();            
            foreach (ListItem item in items)
            {
                HR_AnswerDTO dto = new HR_AnswerDTO(long.Parse(item.Value), item.Text, this.QuestionId, item.Selected);               
                lstAnswer.Add(dto);
            }
          
            question.Answers = lstAnswer;
        
            ///Lấy ý kết quả trả lời dạng nhập
            if (this.txtComment.Visible)
                question.Comment = this.txtComment.Text.Trim();
            else if (this.txtOtherComment.Visible)
                question.Comment = this.txtOtherComment.Text.Trim();
           
            return question;
        }
        /// <summary>
        /// lấy thông tin câu trả lời dạng nhập của enduser
        /// </summary>
        /// <param name="strAnsID"></param>
        /// <param name="strAns"></param>
        /// <returns></returns>
        private HR_QuestionDTO GetOutputQuestion(QuestionsSelectType selectType, string[] strAnsID, string[] strAns)
        {
            HR_QuestionDTO question = new HR_QuestionDTO();
            question.QuestionID = this.QuestionId;
            question.SelectType = selectType;
            question.InputType = QuestionsInputType.None;
            List<HR_AnswerDTO> lstAnswer = new List<HR_AnswerDTO>();       
            //lay ket qua dang nhap
            for (int i = 0; i < strAnsID.Length; i++)
            {
                if (strAns[i] != "" & strAns[i] != null)
                {
                    HR_AnswerDTO dto = new HR_AnswerDTO(long.Parse(strAnsID[i]), strAns[i], this.QuestionId, true);
                    lstAnswer.Add(dto);
                }
            }
            question.Answers = lstAnswer;
            ///Lấy ý kết quả trả lời dạng nhập
            if (this.txtComment.Visible)
                question.Comment = this.txtComment.Text.Trim();
            else if (this.txtOtherComment.Visible)
                question.Comment = this.txtOtherComment.Text.Trim();

            return question;
        }

        public string DataSourceID
        {
            set
            {
               if (this.QuestionSelectType == QuestionsSelectType.Radio)
                    this.dtlRadioAnswerList.DataSourceID = value;
                else if (this.QuestionSelectType == QuestionsSelectType.ComboBox)
                    this.cboComboAnswer.DataSourceID = value;
                else
                    this.dtlCheckBoxAnswerList.DataSourceID = value;
            }
            get
            {
                return this.dtlCheckBoxAnswerList.DataSourceID;
            }
        }

        public override void DataBind()
        {            
            this.dtlCheckBoxAnswerList.DataBind(); 
            this.dtlRadioAnswerList.DataBind();
            
            this.cboComboAnswer.DataBind();
            this.dtlTextboxList.DataBind();
        }

        

      
    }
}